#include "PhysicsManager.h"

#include <Ogre.h>

template<> PhysicsManager* Ogre::Singleton<PhysicsManager>::ms_Singleton = 0;



PhysicsManager* PhysicsManager::getSingletonPtr(void)
{
    return ms_Singleton;
}
PhysicsManager& PhysicsManager::getSingleton(void)
{  
    assert( ms_Singleton );  return ( *ms_Singleton );  
}

PhysicsManager::PhysicsManager()
{
	//Create Bullet stuff.
    mBroadphase = new btAxisSweep3(btVector3(-10000,-10000,-10000), btVector3(10000,10000,10000), 1024);
    mCollisionConfig = new btDefaultCollisionConfiguration();
    mDispatcher = new btCollisionDispatcher(mCollisionConfig);
    mSolver = new btSequentialImpulseConstraintSolver();

    //Create DebugDrawer.
    phyWorld = new btDiscreteDynamicsWorld(mDispatcher, mBroadphase, mSolver, mCollisionConfig);
    phyWorld->setGravity(btVector3(0,0,0));
	shipCollidesWith = COL_WALL;
	wallCollidesWith = COL_NOTHING;
	projectileCollidesWith = COL_WALL;
}

PhysicsManager::~PhysicsManager(void)
{

	//Free Bullet stuff
	delete phyWorld;
    delete mSolver;
    delete mDispatcher;
    delete mCollisionConfig;
    delete mBroadphase;
}

void PhysicsManager::addPhysicsShip(btRigidBody* pObject)
{
	phyWorld->addRigidBody(pObject, COL_SHIP, shipCollidesWith);
}

void PhysicsManager::addPhysicsProjectile(btRigidBody* pObject)
{
	phyWorld->addRigidBody(pObject, COL_PROJECTILE, projectileCollidesWith);
}

void PhysicsManager::removePhysicsObject(btRigidBody* pObject)
{
	phyWorld->removeRigidBody(pObject);
}

void PhysicsManager::updatePhysics(double timeSinceLastFrame)
{
	phyWorld->stepSimulation(timeSinceLastFrame, 10);
}

